package Servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DAO.SanPhamDAO;
import POJO.SanPham;

/**
 * Servlet implementation class AddSanPhamServlet
 */
// @WebServlet("/AddSanPhamServlet")
public class AddSanPhamServlet extends HttpServlet
{
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddSanPhamServlet()
    {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException
    {
        // TODO Auto-generated method stub
        RequestDispatcher dispatcher = request.getRequestDispatcher("/Add/AddSanPham.jsp");
        dispatcher.forward(request, response);

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException
    {
        // TODO Auto-generated method stub
        SanPham sanPham = new SanPham();
        if (request.getParameterValues("themSanPham") != null)
        {
            String[] strs = request.getParameterValues("themSanPham");

            // Chuyển sang chữ có dấu
            strs[0] = new String(strs[0].getBytes("ISO-8859-1"), "UTF-8");
            // Lấy tên
            sanPham.setTenSanPham(strs[0]);

         // Lấy ngày tháng năm
            java.util.Date d = new java.util.Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
            String date = dateFormat.format(d);
            sanPham.setNgayDang(date);
            
            System.out.println(dateFormat.format(d));

            //Lấy mô tả
            strs[1] = new String(strs[1].getBytes("ISO-8859-1"), "UTF-8");
            sanPham.setMoTa(strs[1]);

            //Lấy đơn giá
            sanPham.setDonGia(Float.parseFloat(strs[2]));

            //Lấy số lượng tối thiểu
            sanPham.setSoLuongToiThieu(Integer.parseInt(strs[3]));

            //Lấy số lượng tồn
            sanPham.setSoLuongTon(Integer.parseInt(strs[4]));

            //Lấy Tình trạng
            strs[5] = new String(strs[5].getBytes("ISO-8859-1"), "UTF-8");
            sanPham.setTinhTrang(strs[5]);

            //Lấy mã danh mục
            sanPham.setMaDanhMuc(Integer.parseInt(strs[6]));

            // Thêm sản phẩm
            SanPhamDAO.InsertSanPham(sanPham);

            request.setAttribute("id", "2");
            ArrayList<SanPham> list = SanPhamDAO.SelectSanPhamALL();
            
            request.setAttribute("listSanPham", list);
            RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
            dispatcher.forward(request, response);
        }

    }
}
